﻿using NDK.Motion.MachineManager;
using LightController;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Dlg_LightValueControl : Form
    {
        public Action RefreshUIEventHandler;
        public Dlg_LightValueControl()
        {
            InitializeComponent();
        }

        private static Dlg_LightValueControl _instance;
        private static readonly object singletonLock = new object();
        public static Dlg_LightValueControl Instance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null || _instance.IsDisposed)
                        {
                            _instance = new Dlg_LightValueControl();
                        }
                    }
                }
                return _instance;
            }
        }

        public Light Light
        {
            get
            {
                return _light;
            }
            set
            {
                _light = value;
                LayoutRefresh(value);
            }
        }
        public void LayoutRefresh(Light light)
        {
            if (light != null)
            {
                txt_ImageName.Text = light.Type;
                txt_LightControlName.Text = light.LightControllerName;
                txt_LightName.Text = light.Name;

                txt_Channal.Text = light.LightChanel.No.ToString();
                trackBar_LightValue.Value = light.Value;
                //nud_LightValue.Value = light.Value;

                light.LightController.PropertyChanged += LightController_PropertyChanged;
            }
        }

        private void LightController_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            txt_ControllerStatus.Text = e.PropertyName;
        }

        private Light _light;

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            if (Light != null)
            {
                Light.LightController.Connect();
            }
        }

        private void btnDisConnect_Click(object sender, EventArgs e)
        {
            if (Light != null)
            {
                Light.LightController.Disconnect();
            }
        }


        private void LightValue_ValueChanged(object sender, EventArgs e)
        {
            int LightValue = 0;
            if ((TrackBar)sender as TrackBar != null)
            {
                LightValue = trackBar_LightValue.Value;
                nud_LightValue.Value = LightValue;
            }
            else if ((NumericUpDown)sender as NumericUpDown != null)
            {
                trackBar_LightValue.Value = (int)nud_LightValue.Value;
            }
            LightOn(LightValue);

        }

        private void trackBar_LightValue_Scroll(object sender, EventArgs e)
        {
            int LightValue = trackBar_LightValue.Value;

        }

        private void LightOn(int value)
        {
            if (Light == null)
            {
                txt_ControllerStatus.Text = ("未找到对应光源！");
                return;
            }
            //LightChanel lc = Light?.LightController?.ChanelList?.Find(x => x.No.ToString() == Light.Chanel);
            try
            {
                Light?.LightController?.SetIntensity(Light.LightChanel.No, value);
            }
            catch (Exception ex)
            {
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            Light.Value = (int)nud_LightValue.Value;
            var t = MachineDataManager.Instance.ProjectImageLights;
            txt_ControllerStatus.Text = ("缓存更新成功！");

            if (RefreshUIEventHandler != null)
            {
                RefreshUIEventHandler.Invoke();
            }
        }

        private void nud_LightValue_ValueChanged(object sender, EventArgs e)
        {
            trackBar_LightValue.Value = (int)nud_LightValue.Value;
        }
    }
}
